using NL.Combat;
using UnityEngine;
namespace NL.Advanced
{
    [DefaultExecutionOrder(-1000)]
    public class Passivity : MonoBehaviour
    {
        public int ID;
        public int Priority;
        public string Property;
        public bool PropertyForCombatOwner = true;
        [HideInInspector] public int Level;
        [HideInInspector] public string IconUrl;
        public bool Living = false;

        public ICombatOwner CombatOwner;

        public GameObject ParentLauncherOwner;

        protected IPropertyOwner _PropertyOwner;
        protected NLIni Info;
        protected virtual void Awake()
        {

        }

        bool _STARTED = false;
        protected int? OwerPropertyAnnexID;
        protected void Start()
        {

            OnLive();

            _STARTED = true;
        }

        protected virtual void OnEnable()
        {
            if (_STARTED) Invoke("OnLive", 0.02f);
        }
        protected virtual void OnLive()
        {
            if (CombatOwner == null)
                CombatOwner = GetComponentInParent<ICombatOwner>();


            _LoadInfo();

            AddProperty();

            Living = true;
        }

        protected virtual void AddProperty()
        {
            //添加宿主 属性.
            if (CombatOwner != null && !string.IsNullOrEmpty(Property))
            {
                _PropertyOwner = PropertyForCombatOwner ? CombatOwner : transform.GetComponentInParent<IPropertyOwner>();

                OwerPropertyAnnexID = _PropertyOwner?.GetProperty()?.Add(Property);
            }
        }

        protected virtual void OnDisable()
        {
            //移除宿主 属性.
            if (CombatOwner != null && OwerPropertyAnnexID > 0)
                _PropertyOwner?.GetProperty()?.Remove(OwerPropertyAnnexID.Value);

            Living = false;
        }
        /// <summary>
        /// 加载数据源....
        /// </summary>
        protected virtual void _LoadInfo()
        {
            Info = gameObject.INFO();

            if (Info == null) return;

            //下面是 根据数据源.重置相关 参数.
            ID = Info["id"].AsInt;

            //Property = Info["property"].AsString;

            IconUrl = Info["icon"].AsString;
        }
    }
}

